package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author atguigu-mqx
 * @ClassName SearchReceiver
 * @description: TODO
 * @date 2023年10月30日
 * @version: 1.0
 */
@Component
@Slf4j
public class SearchReceiver {

    @Autowired
    private SearchService searchService;

    /**
     * 专辑上架
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_UPPER)
    public void albumUpper(ConsumerRecord<String,String> record){
        //  获取数据
        Long albumId = Long.valueOf(record.value());
        //  判断
        if (null == albumId){
            log.error("专辑上架失败{}",albumId);
            return;
        }
        //  调用检索的上架方法.
        searchService.upperAlbum(albumId);
    }

    /**
     * 专辑下架
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_LOWER)
    public void albumLower(ConsumerRecord<String,String> record){
        try {
            //  获取数据
            Long albumId = Long.valueOf(record.value());
            //  判断
            if (null != albumId){
                //  调用检索的上架方法.
                searchService.lowerAlbum(albumId);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
